package com.cssl.dao;

import java.time.Duration;
import java.util.HashMap;
import java.util.Map;

import org.springframework.cache.CacheManager;
import org.springframework.cache.annotation.CachingConfigurerSupport;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.cache.RedisCacheConfiguration;
import org.springframework.data.redis.cache.RedisCacheManager;
import org.springframework.data.redis.cache.RedisCacheWriter;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.RedisSerializationContext;

import com.fasterxml.jackson.annotation.JsonAutoDetect;
import com.fasterxml.jackson.annotation.PropertyAccessor;
import com.fasterxml.jackson.databind.ObjectMapper;

@Configuration
@EnableCaching
public class RedisConfig extends CachingConfigurerSupport {

	// 默认缓存管理器
	/*
	 * @Bean public CacheManager cacheManager(RedisConnectionFactory factory) {
	 * RedisCacheManager cacheManager=RedisCacheManager.create(factory); return
	 * cacheManager;
	 * 
	 * }
	 */

	// 缓存管理器
	public CacheManager cacheManager(RedisConnectionFactory factory) {
		// 反序列化
		ObjectMapper om = new ObjectMapper();
		om.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
		om.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);
		GenericJackson2JsonRedisSerializer serializer = new GenericJackson2JsonRedisSerializer(om);
		/*
		 * Jackson2JsonRedisSerializer<Object> serializer = new
		 * Jackson2JsonRedisSerializer<>(Object.class); serializer.setObjectMapper(om);
		 */

		// 设置缓存配置
		RedisCacheConfiguration config = RedisCacheConfiguration.defaultCacheConfig();
		config = config.serializeValuesWith(RedisSerializationContext.SerializationPair.fromSerializer(serializer))
				.entryTtl(Duration.ofSeconds(10)) // 缓存时间,秒
				.disableCachingNullValues() // 不缓存空值
				.prefixKeysWith("user"); // 缓存区间

		RedisCacheConfiguration config2 = RedisCacheConfiguration.defaultCacheConfig();
		config2 = config.serializeValuesWith(RedisSerializationContext.SerializationPair.fromSerializer(serializer))
				.entryTtl(Duration.ofSeconds(60)) // 缓存时间,秒
				.disableCachingNullValues() // 不缓存空值
				.prefixKeysWith("news"); // 缓存区间

		// 对每个缓存空间应用不同的配置
		Map<String, RedisCacheConfiguration> configMap = new HashMap<>();
		configMap.put("user", config);
		configMap.put("news", config2);

		// 初始化一个RedisCacheWriter
		RedisCacheWriter redisCacheWriter = RedisCacheWriter.nonLockingRedisCacheWriter(factory);

		// 初始化RedisCacheManager
		RedisCacheManager cacheManager = new RedisCacheManager(redisCacheWriter, config, configMap);
		return cacheManager;

	}

}
